package com.jash.lifxdemo;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v4.util.Pair;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.jash.lifxdemo.databinding.ActivityMatchBinding;
import com.jash.lifxdemo.databinding.TimeItemBinding;
import com.jash.lifxdemo.entities.ContestEntity;
import com.jash.lifxdemo.entities.ContestShell;
import com.jash.lifxdemo.entities.Entity;
import com.jash.lifxdemo.entities.MatchEntity;
import com.jash.lifxdemo.utils.CommentRecyclerAdapter;
import com.jash.lifxdemo.utils.NetService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MatchActivity extends AppCompatActivity implements Callback<Entity<MatchEntity>>, SwipeRefreshLayout.OnRefreshListener {

    private Calendar calendar;
    private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    private NetService service;
    private CommentRecyclerAdapter<Object> adapter;
    private ActivityMatchBinding binding;
    private TimeItemBinding group;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_match);
        calendar = Calendar.getInstance();
        group = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.time_item, binding.activityMatch, false);
        binding.activityMatch.addView(group.getRoot());
        service = ((BaseApplication) getApplication()).getService();
//        calendar.add(Calendar.DAY_OF_YEAR, -1);
        service.getMatch(SDF.format(calendar.getTime()), SDF.format(calendar.getTime()))
        .enqueue(this);
        Map<Class<?>, Pair<Integer, Integer>> map = new HashMap<>();
        map.put(Date.class, Pair.create(R.layout.time_item, BR.start));
        map.put(ContestEntity.class, Pair.create(R.layout.contest_item, BR.contest));
        adapter = new CommentRecyclerAdapter<>(this, new ArrayList<>(), map);
        binding.matchList.setAdapter(adapter);
        binding.swipe.setOnRefreshListener(this);
        binding.matchList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                View view = recyclerView.getChildAt(0);
                int position = recyclerView.getChildAdapterPosition(view);
                for (int i = position; i >= 0; i--) {
                    Object item = adapter.getItem(i);
                    if (item instanceof Date) {
                        group.setStart((Date) item);
                        break;
                    }
                }
                for (int i = 1; i < recyclerView.getChildCount(); i++) {
                    View childAt = recyclerView.getChildAt(i);
                    if (childAt.getTop() > group.getRoot().getHeight()) {
                        ViewCompat.setTranslationY(group.getRoot(), 0);
                        break;
                    }
                    int p = recyclerView.getChildAdapterPosition(childAt);
                    if (adapter.getItemViewType(p) == R.layout.time_item) {
                        ViewCompat.setTranslationY(group.getRoot(), childAt.getTop() - group.getRoot().getHeight());
                        break;
                    }
                }
            }
        });
    }

    @Override
    public void onResponse(Call<Entity<MatchEntity>> call, Response<Entity<MatchEntity>> response) {
        MatchEntity data = response.body().getData();
//        adapter.add(data.getStart_time());
//        for (ContestShell contestShell : data.getContest_list()) {
//            adapter.add(contestShell.getContest());
//        }
        for (int i = data.getContest_list().size() - 1; i >= 0; i--) {
            adapter.add(0, data.getContest_list().get(i).getContest());
        }
        adapter.add(0, data.getStart_time());
        binding.swipe.setRefreshing(false);
    }

    @Override
    public void onFailure(Call<Entity<MatchEntity>> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
        binding.swipe.setRefreshing(false);
    }

    @Override
    public void onRefresh() {
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        service.getMatch(SDF.format(calendar.getTime()), SDF.format(calendar.getTime()))
                .enqueue(this);
    }
}
